var addxy = document.querySelector('.addxy')
var addXZ = document.querySelector('.addXZ')
var xm = document.querySelector('#xm')
var sr = document.querySelector('#sr')
var zy = document.querySelector('#zy')
var zz = document.querySelector('#zz')
var xf = document.querySelector('#xf')
var ks = document.querySelector('#ks')
var lx = document.querySelector('#lx')
var jf = document.querySelector('#jf')
var jg = document.querySelector('#jg')
var zt = document.querySelector('#zt')
var divsxq = document.querySelector('.div')
var tbodys = document.querySelector('#tbody')
var xzxg = document.querySelector('.xzxg')
var quexg = document.querySelector('.quexg')
var xglx = document.querySelector('.xglx')
var xhjhr = document.querySelector('.xhjhr')
var xxzt = document.querySelector('#xxzt')
var xgxinx = document.querySelector('.xgxinx')
var qxxg = document.querySelector('.qxxg')
var jfjeBox = document.querySelector('.jfjeBox')
var ys = 1
var arr = ['试学', '正常', '休学']
addxy.addEventListener('click', function () {
    BlockN(addXZ)
    xm.value = sr.value = zz.value = xf.value = ks.value = lx.value = jg.value = ''
})
init()

//获取学生信息和专业信息
function init() {
    tbodys.innerHTML = ''
    zy.innerHTML = ''
    zycx.innerHTML = '<option value="">请选择</option>'
    xhr({
        url: 'majors',
        success: function (res) {
            // console.log(res)
            for (var i = 0; i < res.length; i++) {
                zy.innerHTML += `<option value="${res[i].id}">${res[i].name}</option>`
                zycx.innerHTML += `<option value="${res[i].id}">${res[i].name}</option>`
                // console.log(res[i].name)   
            }
        }
    })
    xhr({
        url: 'students?page=' + ys + '&limit=10',
        success: function (res) {
            // console.log(res)
            intt(res)
        }
    })
}
function intt(res){
    for (var i = 0; i < res.length; i++) {
        if (res[i].tuition == res[i].Major.tuition || res[i].tuition > res[i].Major.tuition) {
            if (res[i].Major.totalHours == 0 || res[i].hours == 0) {
                tbodys.innerHTML += `
                <tr>
                <td>${res[i].name}</td>
                <td>${res[i].birthday}</td>
                <td>${res[i].Major.name}</td>
                <td>${res[i].address}</td>
                <td>${res[i].tuition}</td>
                <td>${arr[res[i].status - 1]}</td>
                <td>${res[i].hours}</td>
                <td>${res[i].contact}</td>
                <td>${res[i].paymentTime.split('T')[0]}</td>
                <td>${res[i].guardian}</td>
                <td>
                    <span onclick="xgxx(${res[i].id})">修改</span>
                    <span onclick="xqxx(${res[i].id})">详情</span>
                </td>
            </tr>`
            } else {
                tbodys.innerHTML += `
                <tr>
                <td>${res[i].name}</td>
                <td>${res[i].birthday}</td>
                <td>${res[i].Major.name}</td>
                <td>${res[i].address}</td>
                <td>${res[i].tuition}</td>
                <td>${arr[res[i].status - 1]}</td>
                <td>${res[i].hours}</td>
                <td>${res[i].contact}</td>
                <td>${res[i].paymentTime.split('T')[0]}</td>
                <td>${res[i].guardian}</td>
                <td>
                    <span onclick="xgxx(${res[i].id})">修改</span>
                    <span onclick="xqxx(${res[i].id})">详情</span>
                    <span onclick="sk(${res[i].id})">上课</span>
                </td>
            </tr>`
            }

        } else {
            if (res[i].Major.totalHours == 0 || res[i].Major.totalHours == res[i].hours) {
                tbodys.innerHTML += `
                <tr>
                <td>${res[i].name}</td>
                <td>${res[i].birthday}</td>
                <td>${res[i].Major.name}</td>
                <td>${res[i].address}</td>
                <td>${res[i].tuition}</td>
                <td>${arr[res[i].status - 1]}</td>
                <td>${res[i].hours}</td>
                <td>${res[i].contact}</td>
                <td>${res[i].paymentTime.split('T')[0]}</td>
                <td>${res[i].guardian}</td>
                <td>
                    <span onclick="xgxx(${res[i].id})">修改</span>
                    <span onclick="xqxx(${res[i].id})">详情</span>
                    <span onclick="xfjn(${res[i].id})">学费缴纳</span>
                    
                </td>
            </tr>`
            } else {
                tbodys.innerHTML += `
                <tr>
                <td>${res[i].name}</td>
                <td>${res[i].birthday}</td>
                <td>${res[i].Major.name}</td>
                <td>${res[i].address}</td>
                <td>${res[i].tuition}</td>
                <td>${arr[res[i].status - 1]}</td>
                <td>${res[i].hours}</td>
                <td>${res[i].contact}</td>
                <td>${res[i].paymentTime.split('T')[0]}</td>
                <td>${res[i].guardian}</td>
                <td>
                    <span onclick="xgxx(${res[i].id})">修改</span>
                    <span onclick="xqxx(${res[i].id})">详情</span>
                    <span onclick="xfjn(${res[i].id})">学费缴纳</span>
                    <span onclick="sk(${res[i].id})">上课</span>
                </td>
            </tr>`
            }
        }
    }
}
function ques() {
    NoneB(addXZ)
}

//缴费
var jfjeid = ''
function xfjn(objs) {
    jfjeid = objs
    BlockN(jfjeBox)
}
function jfque() {
    xhr({
        methods: 'PATCH',
        url: 'students/pay/' + jfjeid,
        data: JSON.stringify({
            tuition: Number(jfje.value)
        }),
        success: function (res) {
            console.log(res)
            NoneB(jfjeBox)
            init()
        }
    })
}



//上课
function sk(objs) {
    xhr({
        methods: 'PATCH',
        url: 'students/class/' + objs,
        success: function (res) {
            console.log(res)
            init()
        }
    })
}
function jfqx() {
    NoneB(jfjeBox)
}

//添加学生信息
function addes() {
    var reg = /^1[3456789]\d{9}$/;
    if (xm.value && sr.value && zy.value && zz.value && xf.value && ks.value && lx.value && jf.value && jg.value && zt.value) {
        // console.log('成功')
        if (reg.test(lx.value) == true) {
            xhr({
                methods: 'post',
                url: 'students',
                data: JSON.stringify({
                    name: xm.value,
                    birthday: sr.value,
                    majorId: zy.value,
                    address: zz.value,
                    tuition: Number(xf.value),
                    hours: Number(ks.value),
                    contact: lx.value,
                    paymentTime: jf.value,
                    guardian: jg.value,
                    status: zt.value
                }),
                success: function (res) {
                    console.log(res)
                    NoneB(addXZ)
                    init()
                }
            })
        } else {
            alert('请输入正确的手机号')
        }
    } else {
        alert('请填写完整信息')
    }
}

//为修改选项框添加内容
var xgxxbox = ''
function xgxx(objs) {
    BlockN(xgxinx)
    xglx.value = ''
    xhjhr.value = ''
    // xgxxbox = String(objs)
    xgxxbox = objs
    xhr({
        mthods: 'patch',
        url: 'students',
        data: JSON.stringify({
            id: objs,
        }),
        success: function (res) {
            console.log(res)
            // console.log(res[0].contact)
            for(var i=0;i<res.length;i++){
                if(res[i].id == xgxxbox){
                    xglx.value = res[i].contact
                    xhjhr.value = res[i].guardian
                }
            }
            BlockN(xgxinx)
        }
    })
}
//确认修改
quexg.addEventListener('click', function () {
    xhr({
        methods: 'PATCH',
        url: 'students/' + xgxxbox,
        data: JSON.stringify({
            contact: xglx.value,
            guardian: xhjhr.value,
            status: xxzt.value
        }),
        success: function (res) {
            console.log(res)
            init()
            NoneB(xgxinx)
        }
    })
    // console.log(xglx.value,xhjhr.value,xxzt.value)
})
qxxg.addEventListener('click', function () {
    NoneB(xgxinx)
})
var Id;
function xqxx(obj) {
    Id = obj
    yema[0].value = '5'
    yema[1].value = '5'
    yema[2].value = '5'
    huanye(0)
    huanye(1)
    huanye(2)
    xhr({
        url: `students?id=${obj}`,
        success: function (res) {
            console.log(res)
            BlockN(divsxq)
            for (i = 0; i < res.length; i++) {
                if (res[i].id == Id) {
                    var xinX = document.getElementsByClassName('xinxi')
                    xinX[0].innerHTML = res[i].name
                    xinX[1].innerHTML = res[i].Major.name
                    xinX[2].innerHTML = res[i].address
                    xinX[3].innerHTML = res[i].contact
                    xinX[4].innerHTML = res[i].tuition
                    xinX[5].innerHTML = res[i].guardian
                    if (res[i].status == 1) {
                        res[i].status = '试学'
                    } else if (res[i].status == 2) {
                        res[i].status = '正常'
                    } else if (res[i].status == 3) {
                        res[i].status = '失学'
                    }
                    xinX[6].innerHTML = res[i].status
                }
            }
        }
    })
}
function ret() {
    NoneB(divsxq)
}
//分页
var pageNum = document.querySelector('.pageNum')
function prev() {
    ys -= 1
    if (ys <= 1) {
        ys = 1
    }
    pageNum.innerHTML = `${ys}`
    init()
}
function next() {
    ys += 1
    pageNum.innerHTML = `${ys}`
    init()
}

function cxName(Nam) {
    var cxNam = decodeURIComponent(Nam)
    xhr({
        url: `students?name=${cxNam}`,
        success: function (res) {
            console.log(res)
            tbodys.innerHTML = ''   
            intt(res)
        }
    })
    console.log(Nam)
}
function cxZz(Zz) {
    var cxZz = decodeURIComponent(Zz)
    xhr({
        url: 'students?address=' + cxZz,
        success: function (res) {
            console.log(res)
            tbodys.innerHTML = ''   
            intt(res)
        }
    })
    console.log(Zz)
}
function cxZy(Zy) {
    // console.log(Zy)
    if(Zy == ''){
        init()
    }else{
        xhr({
            url: 'students?majorId=' + Zy,
            success: function (res) {
                console.log(res)
                tbodys.innerHTML = ''
                intt(res)
            }
        })
    }
}




var yeshu = document.getElementsByClassName('yeshu')
var yema = document.getElementsByClassName('yema')
var tbody = document.getElementsByClassName('tbody')
var tanchuang = document.getElementsByClassName('tanchuang')[0]
var nr = document.getElementsByClassName('nr')[0]
var wz;
function tc(fly) {
    wz = fly
    tanchuang.style.display = 'block'
    nr.innerHTML = ''
    if (fly == 0) {
        nr.innerHTML += `<p><input type="text" placeholder='请输入表现' id=bx></p>`
    } else if (fly == 1) {
        nr.innerHTML += `<p><input type="text" placeholder='请输入类型' id=leiX></p><p><input type="text" placeholder='请输入事由' id=yy></p>`
        tanchuang.style.height = '300px'
        tanchuang.style.marginTop = '-150px'
    } else if (fly == 2) {
        nr.innerHTML += `<p><input type="text" placeholder='请输入分数' id=fs></p>`
        tanchuang.style.height = '250px'
        tanchuang.style.marginTop = '-125px'
    }
}
function qvX() {
    tanchuang.style.display = 'none'
}
function tJ() {
    if (wz == 0) {
        var bx = document.getElementById('bx')
        if (bx.value) {
            xhr({
                methods: 'post',
                url: 'performances',
                data: JSON.stringify({
                    StudentId: Id,
                    description: bx.value
                }),
                success: function (res) {
                    console.log(res)
                    NoneB(tanchuang)
                    alert('提交成功')
                    huanye(0)
                }
            })
        } else {
            alert('请输入课堂表现')
        }
        bx.value = ''
    } else if (wz == 1) {
        var leiX = document.getElementById('leiX')
        var yy = document.getElementById('yy')
        if (leiX.value && yy.value) {
            xhr({
                methods: 'post',
                url: 'attendances',
                data: JSON.stringify({
                    StudentId: Id,
                    type: leiX.value,
                    reason: yy.value
                }),
                success: function (res) {
                    console.log(res)
                    NoneB(tanchuang)
                    alert('提交成功')
                    huanye(1)
                }
            })

        } else {
            alert('请把信息输入完整')
        }
        leiX.value = ''
        yy.value = ''
    } else if (wz == 2) {
        var fs = document.getElementById('fs')
        if (fs.value) {
            xhr({
                methods: 'post',
                url: 'scores',
                data: JSON.stringify({
                    StudentId: Id,
                    score: fs.value
                }),
                success: function (res) {
                    console.log(res)
                    NoneB(tanchuang)
                    alert('提交成功')
                    huanye(2)
                }
            })
        } else {
            alert('请输入分数')
        }
        fs.value = ''
    }

}
var yeShu = 1
var yeShu1 = 1
var yeShu2 = 1
var yeM = document.getElementsByClassName('yeM')
function huanye(pd) {
    if (yema[pd].value > 10) {
        alert('输入条数过大')
        yema[pd].value = ''
        return
    }
    if (pd == 0) {
        xhr({
            url: `performances?StudentId=${Number(Id)}&page=` + yeShu + `&limit=` + yema[pd].value,
            success: function (res) {
                console.log(res)
                tbody[pd].innerHTML = ''
                yeM[pd].innerHTML = yeShu
                for (var j = 0; j < res.length; j++) {
                    var sj = res[j].time.split('T')
                    tbody[pd].innerHTML += `<tr><td>${sj[0]}</td><td>${res[j].description}</td></tr>`
                }
            }
        })
    }
    if (pd == 1) {
        xhr({
            url: `attendances?StudentId=${Number(Id)}&page=` + yeShu1 + `&limit=` + yema[pd].value,
            success: function (res) {
                console.log(res)
                tbody[pd].innerHTML = ''
                yeM[pd].innerHTML = yeShu1
                for (var j = 0; j < res.length; j++) {
                    var sj = res[j].time.split('T')
                    tbody[pd].innerHTML += `<tr><td>${sj[0]}</td><td>${res[j].type}</td><td>${res[j].reason}</td></tr>`
                }
            }
        })
    }
    if (pd == 2) {
        xhr({
            url: `scores?StudentId=${Number(Id)}&page=` + yeShu2 + `&limit=` + yema[pd].value,
            success: function (res) {
                console.log(res)
                tbody[pd].innerHTML = ''
                yeM[pd].innerHTML = yeShu2
                for (var j = 0; j < res.length; j++) {
                    var sj = res[j].time.split('T')
                    tbody[pd].innerHTML += `<tr><td>${sj[0]}</td><td>${res[j].score}</td></tr>`
                }
            }
        })
    }

}
function xia(fly) {
    if (fly == 0) {
        yeShu++
        huanye(0)
        yeM[fly].innerHTML = yeShu
    } else if (fly == 1) {
        yeShu1++
        huanye(1)
        yeM[fly].innerHTML = yeShu1
    } else if (fly == 2) {
        yeShu2++
        huanye(2)
        yeM[fly].innerHTML = yeShu2
    }
}
function shang(fly) {
    if (fly == 0) {
        yeShu--
        if (yeShu <= 1) {
            yeShu == 1
        }
        yeM[fly].innerHTML = yeShu
        huanye(0)
    } else if (fly == 1) {
        yeShu1--
        if (yeShu1 <= 1) {
            yeShu1 == 1
        }
        yeM[fly].innerHTML = yeShu1
        huanye(1)
    } else if (fly == 2) {
        yeShu2--
        if (yeShu2 <= 1) {
            yeShu2 == 1
        }
        yeM[fly].innerHTML = yeShu2
        huanye(2)
    }
}